//
//  HLJWorldCell.m
//  B段项目
//
//  Created by 111111 on 15/12/22.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "HLJWorldCell.h"

@implementation HLJWorldCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}


#pragma mark - 子视图
-(void)createSubView{
    
    
    //类型
    self.type = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/20,WIDTH/20, 200, WIDTH/25)];
    self.type.font = [UIFont systemFontOfSize:WIDTH/25];
    self.type.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    [self.contentView addSubview:self.type];
    
    
    //头图
    self.cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.type.origin.x, self.type.origin.y * 2 + self.type.height, WIDTH - self.type.origin.x * 2, HEIGHT/4)];
    [self.contentView addSubview:self.cellImageView];
    
    //标题
    self.titleLb = [[UILabel alloc]initWithFrame:CGRectMake(self.cellImageView.origin.x, self.cellImageView.origin.y + self.cellImageView.height + self.type.origin.y/3*4, self.cellImageView.width, self.type.origin.y)];
    self.titleLb.numberOfLines=2;
    self.titleLb.font = [UIFont boldSystemFontOfSize:self.type.origin.y];
    [self.contentView addSubview:self.titleLb];
    
    
    
    //内容
    self.content = [[UILabel alloc]initWithFrame:CGRectMake(self.titleLb.origin.x, self.titleLb.origin.y + self.titleLb.height +  self.type.origin.y/3*4, self.cellImageView.width, self.type.origin.y)];
    self.content.numberOfLines = 3;
    self.content.font = [UIFont systemFontOfSize:WIDTH/20];
    self.content.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    [self.contentView addSubview:self.content];
    
    
    
    //心形图
    self.heartButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview: self.heartButton];
    
    [self.heartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size. mas_equalTo(CGSizeMake(30, 30));
        // 添加左、上边距约束（左、上约束都是20）
        make.bottom.mas_equalTo(-20);
        
        make.right.mas_equalTo(- 75);
        
        
        
    }];
    
    [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
    
     [self.heartButton addTarget:self action:@selector(heartButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //赞数
    self.zanLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.zanLabel];
    
    [self.zanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size. mas_equalTo(CGSizeMake(35, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.bottom.mas_equalTo(-25);
        
        make.right.mas_equalTo(- 25);
        
        
        
    }];
    
    self.zanLabel.font = [UIFont systemFontOfSize:12];
    self.zanLabel.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    
    
    
    
}



#pragma mark - 内容高度方法
+(CGFloat)heightForContent:(NSString *) content{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:WIDTH/20],NSFontAttributeName, nil];
    CGSize size = CGSizeMake(WIDTH - WIDTH/10, 10000);
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
    
}




#pragma mark - 标题高度方法
+(CGFloat)heightForTitle:(NSString *) title{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:WIDTH/20],NSFontAttributeName, nil];
    
    CGSize size = CGSizeMake(WIDTH - WIDTH/10, 10000);
    
    CGRect frame = [title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
    
}






#pragma mark - set
-(void)setWorldModel:(HLJHomeModel *)worldModel{
    _worldModel = worldModel;
    self.type.text = [NSString stringWithFormat:@"%@ · %@",self.worldModel.name,self.worldModel.enname];
    [self.cellImageView sd_setImageWithURL:self.worldModel.coverimg placeholderImage:PLACEHOLDER_IMAGE];
    self.titleLb.text = self.worldModel.title;
    self.content.text = self.worldModel.content;
    self.zanLabel.text = [NSString stringWithFormat:@"%@",self.worldModel.like];
    
    
    //重新设置title frame
    CGFloat titleHeight = [HLJWorldCell heightForTitle:self.worldModel.title];
    CGRect tepFrame = self.titleLb.frame;
    tepFrame.size.height = titleHeight;
    self.titleLb.frame = tepFrame;
    
    
    self.content.frame = CGRectMake(self.titleLb.origin.x, self.titleLb.origin.y + self.titleLb.height +  self.type.origin.y/3*4, self.cellImageView.width, self.type.origin.y);
    
    
    //重新设置content frame
    CGFloat height = [HLJWorldCell heightForContent:self.worldModel.content];
    CGRect tempFrame = self.content.frame;
    tempFrame.size.height = height;
    self.content.frame = tempFrame;

    //判断收藏状态
    NSMutableArray *array = [[ZZCCollectionData collectDataBase]selectCollectList];
    NSMutableArray *array2 = [NSMutableArray array];
    for (HLJHomeModel *model in array) {
        [array2 addObject:model._id];
    }
    if ([array2 containsObject: self.worldModel._id]) {
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
    }
    
}

- (void)heartButton:(UIButton *)btn
{
    NSMutableArray *array = [[ZZCCollectionData collectDataBase] selectCollectList];
    NSMutableArray *amptyArray = [NSMutableArray array];
    
    for (HLJHomeModel *model in array) {
        [amptyArray addObject:model._id];
    }
    if ([amptyArray containsObject:self.worldModel._id]) {
        [[ZZCCollectionData collectDataBase] deleteMyCollectData:self.worldModel.title];
        
        CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        keyAnimation.values = @[@(0.1),@(1.0),@(1.5)];
        keyAnimation.duration = 0.2;
        [btn.layer addAnimation:keyAnimation forKey:@"key"];
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] - 1];
    }else{
        HLJHomeModel *model = [[HLJHomeModel alloc]init];
        model._id = self.worldModel._id;
        model.title = self.worldModel.title;
        model.coverimg = self.worldModel.coverimg;
        model.name = self.worldModel.name;
        
        [[ZZCCollectionData collectDataBase]inserCollectList:model];
        
        //关键帧动画
        CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        //设置关键帧的值,values是数组类型
        //@(0.1)相当于NSNumber
        keyAnimtion.values = @[@(0.1),@(1.0),@(1.5)];//缩放的倍数
        keyAnimtion.duration = 0.2;
        [btn.layer addAnimation:keyAnimtion forKey:@"key"];
        
        
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] + 1];
    }
    
    
    
}



@end
